<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/5/25
 * Time: 10:45
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Classcate extends Base
{
    /**
     * 课程分类
     * @return mixed|\think\response\Json
     */
    public function cateIndex(){
        if(request()->isAjax ()){
            $where['c_cate_status'] = 1;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('class_cate')->where($where)->count();// 获取总条数
            $lists = Db::name('class_cate')
                ->where($where)
                ->field('c_cate_id, c_cate_name, c_cate_time, c_cate_update_time')
                ->order('c_cate_time desc')
                ->page($Nowpage,$limits)
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加分类
     * @return mixed|\think\response\Json
     */
    public function cateAdd(){
        if(Request::instance()->isPost()){
            $c_cate_name = trim(input('c_cate_name'));
            $res = Db::table('up_class_cate')
                ->insert([
                    'c_cate_name' => $c_cate_name,
                    'c_cate_time' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    
    /**
     * 编辑分类
     * @return mixed|\think\response\Json
     */
    public function cateEdit(){
        $c_cate_id = input('c_cate_id');
        if(Request::instance()->isPost()){
            $c_cate_name = trim(input('c_cate_name'));
            $res = Db::table('up_class_cate')
                ->update([
                    'c_cate_name' => $c_cate_name,
                    'c_cate_id' => $c_cate_id,
                    'c_cate_update_time' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cate = Db::table('up_class_cate')->where('c_cate_id', $c_cate_id)->field('c_cate_name')->find();
        $this->assign([
            'c_cate_id' => $c_cate_id,
            'cate' => $cate
        ]);
        return $this->fetch();
    }
    
    /**
     * 删除分类
     * @return \think\response\Json
     */
    public function cateDel(){
        $id = input('id');
        $res = Db::table('up_class_cate')->update(['c_cate_status' => 2, 'c_cate_id' => $id]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}